<?php
/*
 * ffmpeg操作类
 */
class ffmpeg
{

    //视频长度
    public static function len($room_no, $name)
    {
        $cmd = 'ffmpeg -i ' . DATA . $room_no . DIRECTORY_SEPARATOR . $name . '.mp4 2>&1 | grep \'Duration\' | cut -d \' \' -f 4 | sed s/,//';
        //            var_dump($cmd);
        $content = exec($cmd);
        if (!$content || $content == '') return 0;
        $tmp = explode(':', $content);
        $time = $tmp[0] * 3600 + $tmp[1] * 60 + intval($tmp[2]);
        return $time;
    }

    //视频复制
    public static function copy($room_no, $name, $file)
    {
        $cmd = 'ffmpeg -i ' . $file . ' -s 320*240 ' . DATA . $room_no . DIRECTORY_SEPARATOR . $name . '.mp4';
        //            SayTool::log($cmd);
        return exec($cmd);
    }

    //视频删除
    public static function rm($room_no, $name)
    {
        return unlink(DATA . $room_no . DIRECTORY_SEPARATOR . $name . '.mp4');
    }

    //视频切片
    public static function m3u8($room_no)
    {
        $room_id = str_replace('sanhao', '', $room_no);
        //转换成ts
        $cmd = 'ffmpeg -i ' . DATA . $room_no . DIRECTORY_SEPARATOR . $room_no . '.mp4 -vcodec libx264 ' . DATA . $room_no . DIRECTORY_SEPARATOR . $room_no . '.ts';
        var_dump($cmd);
        $rs = exec($cmd);
        //切片视频
        $cmd = 'segmenter -i ' . DATA . $room_no . DIRECTORY_SEPARATOR . $room_no . '.ts -d 10 -p ' . DATA . $room_no . DIRECTORY_SEPARATOR . 'm3u8/video -m ' . DATA . $room_no . DIRECTORY_SEPARATOR . $room_id . '.m3u8 -u http://video.sanhao.com/record/';
        var_dump($cmd);
        $rs = exec($cmd);
    }
}
